# Author    : CoffeeChicken
# Date      : 2022/1/3 09:39
# Function  : 演示 绘制柱状图

from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.globals import ThemeType
import requests
import json

all_type_hospital = {}

url = "https://y.dxy.cn/papi/hospital/detailInfo?hospital_id=166"

response = requests.get("http://192.168.134.161:8081/hospitals")

hospitals = json.loads(response.text)['hospitals']

for hospital in hospitals:
    if hospital["province"] not in all_type_hospital.keys():
        all_type_hospital[hospital["province"]] = [0 for i in range(8)]
    if hospital["grade"] == "2":
        all_type_hospital[hospital["province"]][0] = all_type_hospital.get(hospital["province"])[0] + 1
    elif hospital["grade"] == "3":
        all_type_hospital[hospital["province"]][1] = all_type_hospital.get(hospital["province"])[1] + 1
    elif hospital["grade"] == "4":
        all_type_hospital[hospital["province"]][2] = all_type_hospital.get(hospital["province"])[2] + 1
    elif hospital["grade"] == "12":
        all_type_hospital[hospital["province"]][3] = all_type_hospital.get(hospital["province"])[3] + 1
    elif hospital["grade"] == "5":
        all_type_hospital[hospital["province"]][4] = all_type_hospital.get(hospital["province"])[4] + 1
    elif hospital["grade"] == "6":
        all_type_hospital[hospital["province"]][5] = all_type_hospital.get(hospital["province"])[5] + 1
    elif hospital["grade"] == "7":
        all_type_hospital[hospital["province"]][6] = all_type_hospital.get(hospital["province"])[6] + 1
    elif hospital["grade"] == "13":
        all_type_hospital[hospital["province"]][7] = all_type_hospital.get(hospital["province"])[7] + 1
print(all_type_hospital)

sanjia = [i[0] for i in list(all_type_hospital.values())]


print(all_type_hospital.keys())
# print(sanjia)

c1 = (
    Bar(init_opts=opts.InitOpts(width="1500px",
                                height="700px",
                                ))
    .add_xaxis(list(all_type_hospital.keys()))
    .add_yaxis("三甲", [i[0] for i in list(all_type_hospital.values())], stack="stack1")
    .add_yaxis("三乙", [i[1] for i in list(all_type_hospital.values())], stack="stack1")
    .add_yaxis("三丙", [i[2] for i in list(all_type_hospital.values())], stack="stack1")
    .add_yaxis("三级", [i[3] for i in list(all_type_hospital.values())], stack="stack1")
    .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
    .set_global_opts(title_opts=opts.TitleOpts(title="三级以上医院数量"))
    .render("hospital_type1.html")
)

c2 = (
    Bar(init_opts=opts.InitOpts(width="1500px",
                                height="700px",
                                theme=ThemeType.MACARONS))
    .add_xaxis(list(all_type_hospital.keys()))
    .add_yaxis("二甲", [i[4] for i in list(all_type_hospital.values())], stack="stack1")
    .add_yaxis("二乙", [i[5] for i in list(all_type_hospital.values())], stack="stack1")
    .add_yaxis("二丙", [i[6] for i in list(all_type_hospital.values())], stack="stack1")
    .add_yaxis("二级", [i[7] for i in list(all_type_hospital.values())], stack="stack1")
    .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
    .set_global_opts(title_opts=opts.TitleOpts(title="二级以上医院数量"))
    .render("hospital_type2.html")
)
